<?php


class MysqlFace extends FaceBase {


	private $nohf = array('exectest');



	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		parent::__construct();
		
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header();
		}

	}

	public function index(){
		$mysqlConf = n_confGet("mysql");
		$rs['conf'] = $mysqlConf['data'];
		$this->_html("index",$rs);
		//print_r($tables);
	}
	
	public function showStatus(){
		$param = $this->param("id:char");
		$rs['id'] = $param['id'];
		$mysqlConf = n_confGet("mysql");
		$rs['conf'] = $mysqlConf['data'];
		if(isset($mysqlConf['data'][$rs['id']])){
			$mysql = n_api("mysql")->$rs['id'];
			
			//$db = n_api("mysql")->db001;
			//$rs['tables'] = $db->select("show tables");
			$rs['status'] = $mysql->select("SHOW STATUS");
			
		}
		$this->_html("showStatus",$rs);
	}
	public function showVar(){
		$param = $this->param("id:char");
		$rs['id'] = $param['id'];
		$mysqlConf = n_confGet("mysql");
		$rs['conf'] = $mysqlConf['data'];
		if(isset($mysqlConf['data'][$rs['id']])){
			$mysql = n_api("mysql")->$rs['id'];
			
			//$db = n_api("mysql")->db001;
			//$rs['tables'] = $db->select("show tables");
			$rs['variables'] = $mysql->select("SHOW VARIABLES");
			
		}
		$this->_html("showVar",$rs);

	}
	public function showProcesslist(){
		$param = $this->param("id:char");
		$rs['id'] = $param['id'];
		$mysqlConf = n_confGet("mysql");
		$rs['conf'] = $mysqlConf['data'];
		if(isset($mysqlConf['data'][$rs['id']])){
			$mysql = n_api("mysql")->$rs['id'];
			
			//$db = n_api("mysql")->db001;
			//$rs['tables'] = $db->select("show tables");
			$rs['processlist'] = $mysql->select("SHOW FULL PROCESSLIST");
			
		}
		$this->_html("showProcesslist",$rs);

	}
	public function showTableStatus(){
		$param = $this->param("id:char");
		$rs['id'] = $param['id'];
		$mysqlConf = n_confGet("mysql");
		$rs['conf'] = $mysqlConf['data'];
		if(isset($mysqlConf['data'][$rs['id']])){
			$mysql = n_api("mysql")->$rs['id'];
			
			$rs['tableStatus'] = $mysql->select("SHOW TABLE STATUS");
			
		}
		$this->_html("showTableStatus",$rs);
	}
	
	public function slowlog(){}
	
	public function errorlog(){}
	
	/**
	 * 测试数据，寿终正寝
	public function datapush(){
		$db = n_api("mysql")->db001;
		
		$fhandle = fopen("/opt/data/xaa.csv",'r');
		fseek($fhandle, rand(0, 190000000));
		fgets($fhandle);
		$content = mb_convert_encoding(fgets($fhandle), "utf-8",'gbk');
		$db->insert("big_data",array("content"=>addslashes($content)));
		
	}
	
	public function dataread(){
		$db = n_api("mysql")->db001;
		
		$ids = array();
		for ($i=0; $i < 100; $i++) { 
			$ids[] = rand(1, 1990000);
		}
		$db->select("select * from big_data where id in(".implode(',', $ids).")");
		// print_r($db->select("select * from big_data where id in(".implode(',', $ids).")"));
	}
	 */

	public function exectest(){
		$param = $this->param("mhandle:char,sql:char");
		
		$mhandle = $param['mhandle'];
		$mysql = n_api("mysql")->$mhandle;
		$sqlArr = explode(' ', $param['sql']);
		$sql = $param['sql'];
		switch(strtolower($sqlArr[0])){
			case 'select':$rs['data'] = $mysql->select($sql);break;
			case 'update':$rs['data'] = $mysql->exec($sql);break;
			case 'delete':$rs['data'] = $mysql->exec($sql);break;
			case 'alter':$rs['data'] = $mysql->exec($sql);break;
			case 'show':$rs['data'] = $mysql->select($sql);break;
			case 'set':$rs['data'] = $mysql->exec($sql);break;
			default:
				throw new PException("错误请求！".json_encode($param));
		}
		
		if(!empty($rs['data'])){
		foreach($rs['data'] as $key=>$val){
			foreach($val as $key1=>$val1){
				$rs['column'][$key1] = $key1;
			}
		}
		}
		
		
		n_mod('skin')->header('simple');
		$this->_html("exectest",$rs);
		n_mod('skin')->footer('simple');
	}

	public function __destruct(){
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->footer();
		}
	}	
}


?>